package xyz.chenyongpeng.userproviderdubbo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.chenyongpeng.entity.ZsUser;
import xyz.chenyongpeng.service.IZsUserService;
import xyz.chenyongpeng.userproviderdubbo.mapper.ZsUserMapper;

import java.util.Map;

public class ZsUserService extends ServiceImpl<ZsUserMapper, ZsUser> implements IZsUserService {

    @Autowired
    private ZsUserMapper zsUserMapper;

    @Override
    public String getUidByOpenid(String openid) {
        return zsUserMapper.getUidByOpenid(openid);
    }

    @Override
    public ZsUser getInfoByOpenid(String openid) {
        return zsUserMapper.selectOne((Wrapper)Wrappers.query().eq("wx_openid", openid));
    }

    @Override
    public String getUserNameByKV(String u_id) {
        return zsUserMapper.getUserNameByKV(u_id);
    }

    @Override
    public String addUser(Map<String, Object> parms) {
        String openid = parms.get("openid").toString();
        String uid = zsUserMapper.getUidByOpenid(openid);
        int new_uid;

        if (uid == null){
            // 新增
            ZsUser zsUser = new ZsUser();
            new_uid = zsUserMapper.insert(zsUser);
        }else {
            ZsUser zsUser = new ZsUser();
            new_uid = zsUserMapper.update(zsUser, (Wrapper)Wrappers.query().eq("u_id", uid));
        }
        return new_uid+"";
    }

    @Override
    public String updateContact(String u_id, String name, String tel) {
        ZsUser zsUser = zsUserMapper.selectOne((Wrapper)Wrappers.query().eq("u_id", u_id));
        zsUser.setUName(name);
        zsUser.setUPhone(tel);
        int new_uid = zsUserMapper.updateById(zsUser);
        return new_uid+"";
    }
}
